package com.wrx.club_management_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wrx.club_management_backend.common.AjaxResult;
import com.wrx.club_management_backend.dto.ActivityDTO;
import com.wrx.club_management_backend.entity.ClubActivity;
import com.wrx.club_management_backend.entity.User;
import com.wrx.club_management_backend.mapper.ClubActivityMapper;
import com.wrx.club_management_backend.mapper.VenueMapper;
import com.wrx.club_management_backend.service.ClubActivityService;
import com.wrx.club_management_backend.service.UserService;
import com.wrx.club_management_backend.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClubActivityServiceImpl extends ServiceImpl<ClubActivityMapper, ClubActivity> implements ClubActivityService {

    @Autowired
    ClubActivityMapper cLubActivityMapper;

    @Autowired
    UserService userService;

    @Autowired
    VenueMapper venueMapper;

    /**
     * 创建活动
     * @param activity
     * @return
     */
    @Override
    public AjaxResult create(ClubActivity activity) {
        this.save(activity);
        venueMapper.insertBooking(activity.getVenueId(), activity.getId(), activity.getBeginData().toString(), activity.getDuration());
        return AjaxResult.success("添加成功");
    }

    @Override
    public AjaxResult hot() {
        List<ClubActivity> list = this.list(
                new LambdaQueryWrapper<ClubActivity>()
                        .eq(ClubActivity::getStatus, 1)
        );
        return AjaxResult.success(list);
    }

    /**
     * 获取活动的详情信息
     * @param id
     * @return
     */
    @Override
    public AjaxResult get(Long id) {
        ClubActivity one = getById(id);
        return AjaxResult.success(one);
    }

    /**
     * 参加活动
     * @param activityId
     * @return
     */
    @Override
    public AjaxResult participate(Long activityId) {
        Long userId = UserHolder.getUser().getId();
        if (! cLubActivityMapper.selectByUserIdAndActivityId(userId, activityId)) { //检查用户是或参加该活动
            cLubActivityMapper.insertActivityParticipant(userId, activityId);
        }
            return AjaxResult.success("报名成功");
    }

    /**
     * 判断是否参加了活动
     * @param activityId
     * @return
     */
    @Override
    public AjaxResult isParticipate(Long activityId) {
        Long userId = UserHolder.getUser().getId(); // 获取用户id
        boolean flag = cLubActivityMapper.selectByUserIdAndActivityId(userId, activityId); // 查询是否已经参加
        return AjaxResult.success(flag); // 返回结果
    }

    /**
     * 根据活动id， 把参加该活动的所有学生查出来
     * @param activityId 活动id
     * @return
     */
    @Override
    public AjaxResult listUserById(Long activityId) {
        List<Long> ids = cLubActivityMapper.listUserById(activityId);
        if(ids.isEmpty()) return AjaxResult.success("没有用户参加");
        List<User> users = userService.listByIds(ids);
        return AjaxResult.success(users);
    }

    @Override
    public AjaxResult num() {
        int size = this.list().size();
        return AjaxResult.success(size);
    }

    @Override
    public AjaxResult approveList() {
        List<ClubActivity> list = cLubActivityMapper.selectList(
                new LambdaQueryWrapper<ClubActivity>()
                        .eq(ClubActivity::getStatus, 0)
        );
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getList(ActivityDTO dto) {
        List<ClubActivity> list = this.list(
                new LambdaQueryWrapper<ClubActivity>()
                        .like( dto.getTitle() != null && !dto.getTitle().isEmpty(), ClubActivity::getTitle, dto.getTitle())
                        .like(dto.getLocation() != null && !dto.getLocation().isEmpty(), ClubActivity::getLocation, dto.getLocation())
                        .eq(dto.getStatus() != null, ClubActivity::getStatus, dto.getStatus())
        );
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult approve(ActivityDTO dto) {
        this.update(
                new LambdaUpdateWrapper<ClubActivity>()
                        .eq(ClubActivity::getId, dto.getActivityId())
                        .set( dto.getStatus() != null, ClubActivity::getStatus, dto.getStatus())
        );
        return AjaxResult.success("修改成功");
    }
}
